<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info"
                  show-icon/>

        <!-- 表格顶部菜单 -->
        <!-- 自定义按钮请使用插槽，甚至公共搜索也可以使用具名插槽渲染，参见文档 -->
        <TableHeader
            :buttons="['refresh', 'add', 'edit', 'delete', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: t('courseorder.quick Search Fields') })"
        >

            <el-button v-auth="'write'" v-blur class="table-header-operate mlr-12" style="margin-left: 12px"
                       type="success" @click="write_show = true">
                <Icon color="#ffffff" name="el-icon-Management"/>
                <span class="table-header-operate-text">核销</span>
            </el-button>

        </TableHeader>

        <!-- 表格 -->
        <!-- 表格列有多种自定义渲染方式，比如自定义组件、具名插槽等，参见文档 -->
        <!-- 要使用 el-table 组件原有的属性，直接加在 Table 标签上即可 -->
        <Table ref="tableRef">




        </Table>

        <!-- 表单 -->
        <PopupForm/>



        <!-- 表单 -->
        <el-dialog
            v-model="write_show"
            title="核销"
            width="500"
        >

            <FormItem :label="t('courseorder.write_code')" style="width: 90%;margin-left: 20px;" type="string" v-model="write_code"
                      :placeholder="t('Please input field', { field: t('courseorder.write_code') })"/>

            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="write_show = false">取消</el-button>
                    <el-button type="primary" @click="sub_write">
                        核销
                    </el-button>
                </div>
            </template>
        </el-dialog>



        <!--考勤-->
        <el-dialog
            v-model="attendance_show"
            title="考勤"
            width="800"
        >

            <FormItem label="上课时间" v-if="attendance_show_sub" style="margin-left: 20px;width: 70%"  type="datetime" v-model="attendance_time" prop="attendance_time" placeholder="请选择上课时间"  />

            <FormItem label="凭证" v-if="attendance_show_sub"  style="margin-left: 48px" type="image" v-model="attendance_image" prop="image" />
            <FormItem label="完成状态" v-if="attendance_show_sub"  style="margin-left: 20px"   type="radio" v-model="attendance_status" :data="{ content: { '0':'未完成','1': '已完成',  } }" />

            <p style="box-sizing: border-box;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: normal;
    word-break: break-all;
    line-height: 23px;
    padding: 0 12px;"> 考勤数 : <span style="color: red;font-size: 16px">{{attendance_num}}</span></p>
            <el-table :data="attendance_data" style="width: 100%" :style="'height:'+attendance_show_w+'px'">
                <el-table-column prop="number" label="序号" width="180" />
                <el-table-column prop="attendance_time" label="上课时间" width="180" />
                <el-table-column prop="image" label="凭证" >

                    <template #default="scope">
                        <div class="demo-image__preview">
                            <el-image
                                style="width: 50px; height: 50px"
                                :src="scope.row.image"
                                :zoom-rate="1.2"
                                :max-scale="7"
                                :min-scale="0.2"
                                :preview-src-list="[scope.row.image]"
                                :initial-index="0"
                                fit="cover"
                                :z-index="99999"
                            />
                        </div>

                    </template>


                </el-table-column>
                <el-table-column prop="create_name" label="记录人" width="180" />
            </el-table>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="attendance_show = false">取消</el-button>

                    <el-button style="margin-left: 5px" v-if="attendance_show_sub" type="primary" @click="sub_attendance">
                        提交
                    </el-button>
                </div>
            </template>
        </el-dialog>


        <!--学生报名资料-->
        <el-dialog
            v-model="student_show"
            title="报名资料"
            width="800"
        >

            <el-descriptions
                title=""
                direction="vertical"
                :column="4"
                border
            >
                <el-descriptions-item label="姓名">{{student_data.name}}</el-descriptions-item>
                <el-descriptions-item label="联系方式">{{student_data.mobile}}</el-descriptions-item>
                <el-descriptions-item label="生日">{{student_data.birthday}}</el-descriptions-item>
                <el-descriptions-item label="年龄">{{student_data.age}}</el-descriptions-item>
                <el-descriptions-item label="性别">{{student_data.sex}}</el-descriptions-item>
                <el-descriptions-item label="健康状况">{{student_data.health}}</el-descriptions-item>
                <el-descriptions-item label="运动基础">{{student_data.exercise}}</el-descriptions-item>
                <el-descriptions-item label="有无基础病">{{student_data.patients}}</el-descriptions-item>
                <el-descriptions-item label="其他说明">{{student_data.explain}}</el-descriptions-item>

            </el-descriptions>


            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="student_show = false">取消</el-button>

                </div>
            </template>
        </el-dialog>

        <!-- 详情组件在此处使用，但显示与否的判断是写在组件内的 -->
        <Info/>
        <After/>
    </div>
</template>

<script setup lang="ts">
import {onMounted, provide, ref} from 'vue'
import {useI18n} from 'vue-i18n'
import PopupForm from './popupForm.vue'
import {baTableApi} from '/@/api/common'
import {defaultOptButtons} from '/@/components/table'
import TableHeader from '/@/components/table/header/index.vue'
import Table from '/@/components/table/index.vue'
import baTableClass from '/@/utils/baTable'
import Info from './info.vue'
import After from './after.vue'
import createAxios from "/@/utils/axios";
import {ElMessage} from "element-plus";
import FormItem from "/@/components/formItem/index.vue";
import {auth} from "/@/utils/common";
import moment from 'moment';

defineOptions({
    name: 'courseorder',
})

const {t} = useI18n()
const tableRef = ref()
const optButtons: OptButton[] = defaultOptButtons(['edit', 'delete'])

const info_auth = auth('info')
const after_auth = auth('after')
const attendance_auth = auth('attendance')
const student_auth = auth('student')
const refund_auth = auth('refund')
optButtons.push(
    {
        render: 'tipButton',
        // name 是任意的
        name: 'info',
        // title 是语言翻译 key
        title: '详情',
        text: '',
        // type: 'warning',
        // icon: 'fa fa-search-plus icon',
        type: 'primary',
        icon: 'fa fa-info-circle',
        click(row, field) {
            // 获取详情
            createAxios({
                url: '/admin/courseorder/info?id='+row.id,
                method: 'get',
                data: {
                    id: row.id,
                }
            }, {
                loading: true
            }, {
                text: '加载中...'
            })
                .then((res) => {
                    if (res.code == 1) {

                        // 在 extend 上自定义一个变量标记详情弹窗显示状态，详情组件内以此判断显示即可！

                        baTable.table.extend!.showInfo = true
                        baTable.table.extend!.infoData = res.data.row
                    } else {

                        ElMessage({
                            message: res.msg,
                            type: 'error',
                        })
                    }


                })
                .catch((resx) => {
                })
        },

        display: (row, field) => {
            if (info_auth){
                return true;
            }
        },
    },
    {
        render: 'tipButton',
        // name 是任意的
        name: 'after',
        // title 是语言翻译 key
        title: '售后',
        text: '',
        type: 'warning',
        // icon: 'fa fa-search-plus icon',
        icon: 'el-icon-Failed',
        click(row, field) {
            // 获取详情
            createAxios({
                url: '/admin/courseorder/after?id='+row.id,
                method: 'get',
                data: {
                }
            }, {
                loading: true
            }, {
                text: '加载中...'
            })
                .then((res) => {
                    if (res.code == 1) {

                        // 在 extend 上自定义一个变量标记详情弹窗显示状态，详情组件内以此判断显示即可！

                        baTable.table.extend!.showAfter = true
                        baTable.table.extend!.infoData = res.data.row
                    } else {

                        ElMessage({
                            message: res.msg,
                            type: 'error',
                        })
                    }


                })
                .catch((resx) => {
                })
        },

        display: (row, field) => {
            if (after_auth && row.order_status == 60){
                return true;
            }
        },
    },
    {
        render: 'tipButton',
        // name 是任意的
        name: 'student',
        // title 是语言翻译 key
        title: '报名资料',
        text: '',
        type: 'danger',
        // icon: 'fa fa-search-plus icon',
        icon: 'el-icon-UserFilled',
        click(row, field) {

            // 获取
            createAxios({
                url: '/admin/courseorder/student?id='+row.id,
                method: 'get',
                data: {
                }
            }, {
                loading: true
            }, {
                text: '加载中...'
            })
                .then((res) => {
                    if (res.code == 1) {
                        student_data.value = res.data.row
                        student_show.value = true;
                    }
                })
                .catch((resx) => {
                })
        },

        display: (row, field) => {
            if (student_auth && row.course_type == 1){
                return true;
            }
        },
    },
    {
        render: 'tipButton',
        // name 是任意的
        name: 'attendance',
        // title 是语言翻译 key
        title: '考勤',
        text: '',
        type: 'success',
        // icon: 'fa fa-search-plus icon',
        icon: 'el-icon-Cpu',
        click(row, field) {
            id.value = row.id
            if ( row.order_status == 10 || row.order_status == 20){
                attendance_show_sub.value = true;
                attendance_show_w.value = 250;
            }else {
                attendance_show_sub.value = false;
                attendance_show_w.value = 400;
            }
            // 获取考勤
            createAxios({
                url: '/admin/courseorder/attendance?id='+row.id,
                method: 'get',
                data: {
                }
            }, {
                loading: true
            }, {
                text: '加载中...'
            })
                .then((res) => {
                    if (res.code == 1) {

                        attendance_data.value = res.data.attendance
                        attendance_num.value = res.data.attendance_num;
                        attendance_show.value = true;
                        attendance_status.value = 0;
                    } else {

                        ElMessage({
                            message: res.msg,
                            type: 'error',
                        })
                    }


                })
                .catch((resx) => {
                })
        },

        display: (row, field) => {
            if (attendance_auth && row.order_status >= 10 && row.order_status <= 60 && row.course_type == 1){
                return true;
            }
        },
    },

    {
        render: 'confirmButton',
        name: 'refund_money',
        title: '退款',
        // text: '退款',
        type: 'info',
        icon: 'fa fa-sign-out',
        class: 'table-row-info',
        popconfirm: {
            title:'确认退款吗？',
        },

        click(row, field) {
            // 获取详情
            createAxios({
                url: '/admin/courseorder/refund',
                method: 'post',
                data: {
                    id:row.id,
                }
            }, {
                loading: true
            }, {
                text: '退款中...'
            })
                .then((res) => {
                    ElMessage({
                        message: res.msg,
                        type: 'success',
                    })

                })
                .catch((resx) => {
                })
        },

        display: (row, field) => {
            if (refund_auth && row.refund_status == 3){
                return true;
            }
        },
    },
)

/**
 * baTable 内包含了表格的所有数据且数据具备响应性，然后通过 provide 注入给了后代组件
 */
const baTable = new baTableClass(
    new baTableApi('/admin/Courseorder/'),
    {
        pk: 'id',
        column: [
            {type: 'selection', align: 'center', operator: false},
            {
                label: t('courseorder.order_type'),
                prop: 'order_type',
                align: 'center',
                operator: 'eq',
                sortable: false,
                replaceValue: {'1': t('courseorder.order_type 1'), '2': t('courseorder.order_type 2')},
                render: 'tag'
            },
            // { label: t('courseorder.id'), prop: 'id', align: 'center', width: 70, operator: 'RANGE', sortable: 'custom' },
            {
                label: t('courseorder.user__nickname'),
                width: 100,
                prop: 'user.nickname',
                align: 'center',
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE'
            },
            // { label: t('courseorder.shop_id'), prop: 'shop_id', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE' },
            {
                label: t('courseorder.shop__name'),
                width: 150,
                prop: 'shop.name',
                align: 'center',
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE'
            },
            // { label: t('courseorder.coach_id'), prop: 'coach_id', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE' },
            {
                label: t('courseorder.coach__name'),
                width: 100,
                prop: 'coach.name',
                align: 'center',
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE'
            },
            {
                label: t('courseorder.market__name'),
                width: 100,
                prop: 'market.name',
                align: 'center',
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE'
            },
            {
                label: t('courseorder.course__name'),
                width: 150,
                prop: 'name',
                align: 'center',
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE'
            },
            {
                label: t('courseorder.order_no'),
                width: 200,
                prop: 'order_no',
                align: 'center',
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE',
                sortable: false
            },
            // { label: t('courseorder.pay_order'), prop: 'pay_order', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            // { label: t('courseorder.name'),width: 150, prop: 'name', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            {
                label: t('courseorder.course_type'),
                width: 100,
                prop: 'course_type',
                align: 'center',
                render: 'tag',
                operator: 'eq',
                sortable: false,
                replaceValue: {'1': t('courseorder.course_type 1'), '2': t('courseorder.course_type 2')}
            },
            // { label: t('courseorder.image'), prop: 'image', align: 'center', render: 'image', operator: false },
            {
                label: t('courseorder.price'),
                width: 100,
                prop: 'price',
                align: 'center',
                operator: false,
                sortable: false
            },
            { label: t('courseorder.num'),width: 100, prop: 'num', align: 'center', operator: 'RANGE', sortable: false },
            {
                label: t('courseorder.vip_price'),
                width: 100,
                prop: 'vip_price',
                align: 'center',
                operator: false,
                sortable: false
            },
            {
                label: t('courseorder.pay_money'),
                width: 100,
                prop: 'pay_money',
                align: 'center',
                operator: false,
                sortable: false
            },
            // { label: t('courseorder.pay_type'), prop: 'pay_type', align: 'center', operator: 'eq', sortable: false, replaceValue: { '1': t('courseorder.pay_type 1'), '2': t('courseorder.pay_type 2'), '3': t('courseorder.pay_type 3'), '9': t('courseorder.pay_type 9') } },
            // { label: t('courseorder.pay_status'), prop: 'pay_status', align: 'center', render: 'tag', operator: 'eq', sortable: false, replaceValue: { '0': t('courseorder.pay_status 0'), '1': t('courseorder.pay_status 1') } },
            {
                label: t('courseorder.order_status'),
                width: 100,
                prop: 'order_status',
                align: 'center',
                render: 'tag',
                operator: 'eq',
                sortable: false,
                replaceValue: {
                    '1': t('courseorder.order_status 1'),
                    '10': t('courseorder.order_status 10'),
                    '20': t('courseorder.order_status 20'),
                    '30': t('courseorder.order_status 30'),
                    '40': t('courseorder.order_status 40'),
                    '50': t('courseorder.order_status 50'),
                    '60': t('courseorder.order_status 60'),
                    '90': t('courseorder.order_status 90')
                }
            },

            {
                label: t('courseorder.after_status'),
                width: 100,
                prop: 'after_status',
                align: 'center',
                render: 'tag',
                operator: 'eq',
                sortable: false,
                replaceValue: {
                    '0': t('courseorder.after_status 0'),
                    '1': t('courseorder.after_status 1'),
                    '2': t('courseorder.after_status 2'),
                    '3': t('courseorder.after_status 3')
                }
            },

            { label: t('courseorder.refund_status'),width:100, prop: 'refund_status', render: 'tag', align: 'center', operator: 'eq', sortable: false, replaceValue: { '0': t('courseorder.refund_status 0'), '1': t('courseorder.refund_status 1'), '2': t('courseorder.refund_status 2'), '3': t('courseorder.refund_status 3'), '4': t('courseorder.refund_status 4') } },

            {
                label: t('courseorder.pay_time'),
                prop: 'pay_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                sortable: 'custom',
                width: 160,
                timeFormat: 'yyyy-mm-dd hh:MM:ss'
            },
            {
                label: t('courseorder.use_time'),
                prop: 'use_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                sortable: 'custom',
                width: 160,
                timeFormat: 'yyyy-mm-dd hh:MM:ss'
            },
            // { label: t('courseorder.attendance_time'), prop: 'attendance_time', align: 'center', render: 'datetime', operator: 'RANGE', sortable: 'custom', width: 160, timeFormat: 'yyyy-mm-dd hh:MM:ss' },
            {
                label: t('courseorder.confirm_time'),
                prop: 'confirm_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                sortable: 'custom',
                width: 160,
                timeFormat: 'yyyy-mm-dd hh:MM:ss'
            },
            // { label: t('courseorder.cancel_time'), prop: 'cancel_time', align: 'center', render: 'datetime', operator: 'RANGE', sortable: 'custom', width: 160, timeFormat: 'yyyy-mm-dd hh:MM:ss' },
            // { label: t('courseorder.team_id'), prop: 'team_id', align: 'center', operator: 'RANGE', sortable: false },
            // { label: t('courseorder.sub_after_time'), prop: 'sub_after_time', align: 'center', render: 'datetime', operator: 'RANGE', sortable: 'custom', width: 160, timeFormat: 'yyyy-mm-dd hh:MM:ss' },
            // { label: t('courseorder.after_check_time'), prop: 'after_check_time', align: 'center', render: 'datetime', operator: 'RANGE', sortable: 'custom', width: 160, timeFormat: 'yyyy-mm-dd hh:MM:ss' },
            // { label: t('courseorder.refund_money'),width: 100, prop: 'refund_money', align: 'center', operator: 'RANGE', sortable: false },
            // { label: t('courseorder.refund_status'), prop: 'refund_status', align: 'center', render: 'tag', operator: 'eq', sortable: false, replaceValue: { '0': t('courseorder.refund_status 0'), '1': t('courseorder.refund_status 1'), '2': t('courseorder.refund_status 2'), '3': t('courseorder.refund_status 3') } },
            // { label: t('courseorder.qrcode'), prop: 'qrcode', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            // { label: t('courseorder.update_time'), prop: 'update_time', align: 'center', render: 'datetime', operator: 'RANGE', sortable: 'custom', width: 160, timeFormat: 'yyyy-mm-dd hh:MM:ss' },
            // { label: t('courseorder.user_del_time'), prop: 'user_del_time', align: 'center', render: 'datetime', operator: 'RANGE', sortable: 'custom', width: 160, timeFormat: 'yyyy-mm-dd hh:MM:ss' },
            {
                label: t('courseorder.create_time'),
                prop: 'create_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                sortable: 'custom',
                width: 160,
                timeFormat: 'yyyy-mm-dd hh:MM:ss'
            },
            {
                label: t('Operate'),
                align: 'center',
                width: 100,
                render: 'buttons',
                buttons: optButtons,
                operator: false,
                fixed: 'right'
            },
        ],
        dblClickNotEditColumn: false,
    },
    {
        defaultItems: {
            order_type: '1',
            course_type: '1',
            price: 0,
            vip_price: 0,
            pay_money: 0,
            pay_time: null,
            pay_type: '1',
            pay_status: '1',
            order_status: '1',
            use_time: null,
            attendance_time: null,
            confirm_time: null,
            cancel_time: null,
            team_id: 0,
            after_status: '0',
            sub_after_time: null,
            after_check_time: null,
            refund_money: 0,
            user_del_time: null
        },
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})


/**
 * 核销
 */
const write_show = ref(false);
const write_code = ref();
const sub_write = () => {

    createAxios({
        url: '/admin/courseorder/write',
        method: 'post',
        data: {
            write_code: write_code.value,
        }
    }, {
        loading: true
    }, {
        text: '核销中...'
    })
        .then((res) => {
            ElMessage({
                message: res.msg,
                type: 'success',
            })

            write_show.value = false;
            write_code.value = '';

            baTable.onTableHeaderAction('refresh', {})

        })
        .catch((resx) => {
        })
}

const id = ref(0);
// 考勤
const attendance_show = ref(false);
const attendance_num = ref(0);
const attendance_data = ref([]);
const attendance_time = ref(moment(new Date()).format('YYYY-MM-DD HH:mm:ss'));
const attendance_image = ref();
const attendance_status = ref(0);
const attendance_show_sub = ref(false);
const attendance_show_w = ref(400);
const sub_attendance = () => {

    createAxios({
        url: '/admin/courseorder/attendance',
        method: 'post',
        data: {
            id: id.value,
            attendance_time: attendance_time.value,
            image: attendance_image.value,
            attendance_status: attendance_status.value,
        }
    }, {
        loading: true
    }, {
        text: '提交中...'
    })
        .then((res) => {
            ElMessage({
                message: res.msg,
                type: 'success',
            })


            attendance_show.value = false;
            attendance_time.value = moment(new Date()).format('YYYY-MM-DD HH:mm:ss');
            attendance_image.value = '';

            baTable.onTableHeaderAction('refresh', {})


        })
        .catch((resx) => {
        })
}

/**
 * 学生资料
 */
const student_show = ref(false);
const student_data = ref({});
</script>

<style scoped lang="scss"></style>
